package com.qiyun.dinghuohui.utils;

import com.qiyun.dinghuohui.base.BaseActivity;

import java.util.Stack;


public class PublicActivityManager {
	private int activityStackCount = 0;
	private static Stack<BaseActivity> activityStack;
	private static PublicActivityManager instance;

	private PublicActivityManager() {
	}

	public static PublicActivityManager getScreenManager() {
		if (instance == null) {
			instance = new PublicActivityManager();
		}
		return instance;
	}

	// 退出栈顶Activity
	public void popActivity(BaseActivity activity) {
		if (activity != null) {
			// 在从自定义集合中取出当前Activity时，也进行了Activity的关闭操作
			activity.finish();
			activityStack.remove(activity);
			activity = null;
			activityStackCount--;
		}
	}

	// 获得当前栈顶Activity
	public BaseActivity currentActivity() {
		BaseActivity activity = null;
		if (activityStack == null)
			return null;
		if (!activityStack.empty()) {
			activity = activityStack.lastElement();
		}
		return activity;
	}

	// 将当前Activity推入栈中
	public void pushActivity(BaseActivity activity) {
		if (activityStack == null) {
			activityStack = new Stack<BaseActivity>();
		}
		activityStack.add(activity);
		activityStackCount++;
	}

	// 退出栈,保留一个
	public void popActivitySaveOne(Class cls) {
		while (activityStackCount > 1) {
			BaseActivity activity = currentActivity();
			if (activity == null) {
				break;
			}
			if (activity.getClass().equals(cls)) {
				break;
			}
			popActivity(activity);
		}
	}

	// 退出栈中所有Activity
	public void popAllActivityExceptOne(Class cls) {
		while (true) {
			BaseActivity activity = currentActivity();
			if (activity == null) {
				break;
			}
			if (activity.getClass().equals(cls)) {
				break;
			}
			popActivity(activity);
		}
	}
}
